package com.tuo51.manager.controller;


import com.tuo51.evaluate.domain.evaluatingV2.EvaluateV2DimensionRecommendTips;
import com.tuo51.evaluate.repositories.evaluatev2.EvaluateV2DimensionRecommendTipsRepository;
import com.tuo51.evaluate.vo.PBDimensionRecommendTipsVO;
import com.tuo51.foudation.exception.BusinessException;
import com.tuo51.foudation.model.ErrorCode;
import com.tuo51.foudation.model.MemVerificationCache;
import com.tuo51.foudation.model.StatusResponse;
import com.tuo51.foudation.utils.CopyUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import static com.tuo51.foudation.constant.RequestConstant.TICKET;
import static org.springframework.web.bind.annotation.RequestMethod.GET;
import static org.springframework.web.bind.annotation.RequestMethod.POST;

/**
 * 附近的合作伙伴
 */
@RestController
@RequestMapping("/mapi/pb_d_recommend")
@Api(value = "/mapi/pb_d_recommend", tags = "OP - 领域内容推荐")
public class MAPIPbDimensionRecommendController {
    @Autowired
    private MemVerificationCache cache;

    @Autowired
    private EvaluateV2DimensionRecommendTipsRepository tipsRepository;

    @RequestMapping(value = "/list", method = GET)
    @ApiOperation(value = "资源列表")
    public StatusResponse list(@RequestHeader(TICKET) String ticket,
                               @RequestParam(value = "pageNum", required = false, defaultValue = "0")Integer pageNum,
                               @RequestParam(value = "pageSize", required = false, defaultValue = "15")Integer pageSize,
                               @RequestParam(value = "searchBy", required = false) String searchBy){

        cache.getStaffId(ticket);

        PageRequest pageRequest = PageRequest.of(pageNum, pageSize);

        Page<EvaluateV2DimensionRecommendTips> ret = tipsRepository.findAll(new Specification<EvaluateV2DimensionRecommendTips>() {
            @Override
            public Predicate toPredicate(Root<EvaluateV2DimensionRecommendTips> root, CriteriaQuery<?> query, CriteriaBuilder cb) {

                Predicate base = cb.isNotNull(root.get("id"));

                if (StringUtils.isNotEmpty(searchBy)){
                    String key = "%" + searchBy + "%";
                    Predicate p = cb.like(root.get("channel"), key);
                    base = cb.and(base, p);
                }

                base = cb.and(base, cb.isFalse(root.get("deleted")));

                query.where(base);

                query.orderBy(cb.desc(root.get(" ")));

                return query.getRestriction();
            }
        }, pageRequest);

        return StatusResponse.success(ret);
    }

    @RequestMapping(value = "/edit", method = POST)
    @ApiOperation(value = "编辑 或 创建")
    public StatusResponse edit(@RequestHeader(TICKET) String ticket,
                               PBDimensionRecommendTipsVO item){
        cache.getStaffId(ticket);

        cache.getStaffId(ticket);

        EvaluateV2DimensionRecommendTips ret = null;
        if (item.getId() != null){
            ret = tipsRepository.getOne(item.getId());
        }else{
            ret = new EvaluateV2DimensionRecommendTips();
        }
        CopyUtils.copyProperties(item, ret);

        tipsRepository.save(ret);

        return StatusResponse.success(ret);
    }


    @RequestMapping(value = "/delete", method = POST)
    @ApiOperation(value = "删除")
    public StatusResponse delete(@RequestHeader(TICKET) String ticket,
                                 String id){
        cache.getStaffId(ticket);

        if (StringUtils.isEmpty(id)){
            throw new BusinessException(ErrorCode.PARAM_NOT_BLANK, "参数错误");
        }

        EvaluateV2DimensionRecommendTips item = tipsRepository.getOne(id);
        if (item == null){
            throw new BusinessException(ErrorCode.MATERIAL_NOT_EXIST, "对象不存在");
        }

        item.setDeleted(true);
        tipsRepository.save(item);

        return StatusResponse.success(true);
    }
}
